
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: '',
        list: [{
            id: -1,
            text: '全部',
            page: 1,
            isFinish: false,
            orderList: []
        },
        {
            id: 0,
            text: '待付款',
            page: 1,
            isFinish: false,
            orderList: []
        },
        {
            id: 1,
            text: '待发货',
            page: 1,
            isFinish: false,
            orderList: []
        },
        {
            id: 2,
            text: '待收货',
            page: 1,
            isFinish: false,
            orderList: []
        },
        {
            id: 3,
            text: '已完成',
            page: 1,
            isFinish: false,
            orderList: []
        },
        {
            id: 4,
            text: '已取消',
            page: 1,
            isFinish: false,
            orderList: []
        },
        ],
        selectIndex: -1,
        size: 10,
        path: app.globalData.path
    },

    toPage(e) {
        console.log(e);
        var obj = e.currentTarget.dataset.obj,
            flag = e.currentTarget.dataset.flag;

        if (flag == 'orderDetail' || flag == 'pay') {
            wx.navigateTo({
                url: '/packageProduct/pages/orderDetail/orderDetail?order=' + JSON.stringify(obj),
            })
        } else if (flag == 'cancel') {
            // 取消订单
            console.log(obj.orderNumber);
            this.cancelOrConfirmOrderHandle(obj.mainId, 4)
        } else if (flag == 'wuLiu') {
            // 查看物流
            console.log(obj.orderNumber);
            wx.navigateTo({
                url: '/userPackage/pages/orderWuLiu/orderWuLiu?orderNum=' + obj.orderNumber,
            })
        } else if (flag == 'shouhuo') {
            // 确认收货
            this.cancelOrConfirmOrderHandle(obj.mainId, 3)
        } else if (flag == 'pingjia') {
            // 去评价
            wx.navigateTo({
                url: '/userPackage/pages/orderDetail/orderDetail?order=' + JSON.stringify(obj),
            })
        } else if (flag == 'del') {
            // 删除订单
            this.cancelOrConfirmOrderHandle(obj.mainId, 4)
        } else if (flag == 'returnGoods') {
            wx.navigateTo({
                url: '/orderPackage/pages/returnGoods/returnGoods?order=' + JSON.stringify(obj) + '&flag=1',
            })
        }
    },
    // 取消订单 确认收货
    cancelOrConfirmOrderHandle(orderId, orderStatus) {
        console.log(orderId);
        console.log(orderStatus);
        var url = app.globalData.url + "order/editOrderStatus",
            that = this,
            list = this.data.list;
        wx.showModal({
            title: '提示',
            content: orderStatus == 4 ? '是否操作该订单' : '是否确认收货',
            complete: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '',
                    })
                    app.wxRequest(url, {
                        mainId: orderId,
                        orderStatus
                    }, res => {
                        console.log(res);
                        if (res.flag) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '操作成功',
                                showCancel: false,
                                complete: (res) => {
                                    if (res.confirm) {
                                        var orderIndex = -1,
                                            obj = '';
                                        console.log(orderStatus);
                                        console.log(list);
                                        if (orderStatus == 4) {
                                            list[1].orderList.forEach((ele, index) => {
                                                if (ele.mainId == orderId) {
                                                    orderIndex = index
                                                    obj = ele
                                                }
                                            })
                                            obj.orderStatus = 4
                                            list[1].orderList.splice(orderIndex, 1)
                                            list[5].orderList.unshift(obj)
                                        } else {
                                            list[3].orderList.forEach((ele, index) => {
                                                if (ele.mainId == orderId) {
                                                    orderIndex = index
                                                    obj = ele
                                                }
                                            })
                                            console.log(orderIndex);
                                            console.log(obj);
                                            obj.orderStatus = 3
                                            list[3].orderList.splice(orderIndex, 1)
                                            list[4].orderList.unshift(obj)
                                        }
                                        console.log(list);
                                        this.setData({
                                            list
                                        })
                                    }
                                }
                            })
                        }
                    },
                        err => {
                            console.log(err);
                        })
                }
            }
        })
    },


    onChange(e) {
        console.log(e);
        this.setData({
            active: e.detail.name,
            selectIndex: this.getSelectIndex(e.detail.name)
        })
        console.log(this.data.active);
        console.log(this.data.list);
        console.log(this.data.list[this.data.selectIndex].orderList);

        if (this.data.list[this.data.selectIndex].orderList.length == 0 && this.data.list[this.data.selectIndex].page == 1) {
            this.getOrder()
        }
    },

    getSelectIndex(id) {
        console.log(id);
        var list = this.data.list,
            selectIndex = -1;
        list.forEach((item, index) => {
            if (item.id == id) {
                selectIndex = index
            }
        })
        console.log(selectIndex);
        return selectIndex
    },


    getOrder() {
        wx.showLoading({
            title: '',
        })
        var url = app.globalData.url + "order/listOrder",
            list = this.data.list,
            active = this.data.active,
            selectIndex = this.data.selectIndex,
            that = this;
        console.log(list);
        console.log(active);
        console.log(selectIndex);

        app.wxRequest(url, {
            flag: active,
            page: list[selectIndex].page,
            size: that.data.size,
            userId: app.globalData.userId,
            shopId: app.globalData.shopId
        }, res => {
            console.log(res);
            if (res.flag) {
                var datas = res.data;
                console.log(selectIndex);
                console.log(datas.list);
                console.log(datas.list.length);
                list[selectIndex].isFinish = datas.list.length < that.data.size
                list[selectIndex].orderList.push(...datas.list)
                list[selectIndex].page++
                console.log(datas);
                wx.hideLoading()
                wx.stopPullDownRefresh()

                that.setData({
                    list
                })
            }
        },
            err => {
                console.log(err);
            })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var id = Number(options.index || -1)
        this.setData({
            active: id,
            selectIndex: this.getSelectIndex(id)
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getOrder()
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        var list = this.data.list,
            selectIndex = this.data.selectIndex;
        list[selectIndex].page = 1
        list[selectIndex].isFinish = false
        list[selectIndex].orderList = []

        console.log(selectIndex);

        this.setData({
            list
        })
        this.getOrder()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var list = this.data.list,
            selectIndex = this.data.selectIndex;
        if (list[selectIndex].isFinish) {
            wx.showToast({
                title: '暂无更多订单',
                icon: 'none'
            })
            return
        } else {
            this.getOrder()
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})